<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Cart;

class ShoplistController extends Controller
{
    /**
     *index 店铺列表展示
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        //数据库查询
        // $res = DB::select('select * from shoptypes');

        if(empty($_GET['search']) && empty(session('search'))){
            return redirect('/');
        }
        if(!empty($_GET['search'])){
            session(['search'=>$_GET['search']]);
        }
        $search = isset($_GET['search']) ? $_GET['search'] : session('search');

        
        if(!empty($_GET['city'])){
            $city = $_GET['city'];
            session(['city'=>$city]);
        }else{
            if(!empty(session('city'))){
                $city = session('city');
            }else{
                $city = '北京';
                session(['city'=>$city]);
            }
        }
        
        $dizhi = $city.' > '.$search;
        session(['dizhi'=>$dizhi]);
        $res = DB::select("select * from shops where shopstatus = '0'");

        // dd($res);

        return view('home.shoplist',['sac'=>$res,'dizhi'=>$dizhi]);
    }

    /**
     *create 是店铺内部详情展示
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 获取店铺ID
        $id = $_GET['id'];

        // 获取店铺详情
        $rs = DB::select("select * from shops where id='$id'");
     
        //获取商品分类
        $res = DB::select("select * from shoptypes where sid='$id'");
        
        
        $arr = array();
        foreach ($res as $k => $v){
            // dd($v->typename);die;
            $asc = DB::select("select * from goods where stid='$v->id'");
            foreach ($asc as $key => $value) {
                // var_dump($value->goodsname);die;
                $arr[$v->typename][] = $value;
            }

        //     
        }


        // $carts = Cart :: content();
        Cart :: destroy();
        return view('home.shopcenter',['res'=>$arr,'asc'=>$rs,'type'=>$res,'sid'=>$id]);

        // dd($arr);die;
    }

    /**
     *shopcomment 是美食根据评价的排序列表
     *
     */
    public function shopcomment()
    {
        echo '评价';

    }

    /**
     *shopsales 根据销量排序
     *
     */
    public function shopsales()
    {
        $id = $_GET['id'];

        // 获取店铺详情
        $rs = DB::select("select * from shops where id='$id'");
        //获取商品分类
        $res = DB::select("select * from shoptypes where sid='$id'");

        $ras = DB::select("select * from goods where sid='$id' order by goodscount desc");

        // dd($ras);
        return view('home.shopprice',['ras'=>$ras,'type'=>$res,'asc'=>$rs]);
    }

    /**
     *shopprice 根据价格排序
     *
     */
    public function shopprice()
    {
        
         // 获取店铺ID
        $id = $_GET['id'];

        // 获取店铺详情
        $rs = DB::select("select * from shops where id='$id'");
        //获取商品分类
        $res = DB::select("select * from shoptypes where sid='$id'");

        $ras = DB::select("select * from goods where sid='$id' order by price desc");

        // dd($ras);
        return view('home.shopprice',['ras'=>$ras,'type'=>$res,'asc'=>$rs]);
        
    }

    /**
     *comment 店铺评论
     *
     */
    public function cartcart()
    {
        $cart = Cart :: content();
        foreach($cart as $v){
            echo $v->options->size.'sid<br>';
            echo $v->id.'id<br>';
            
        }
        

    }

    /**
     *cart gouwuche 
     *
     */
    public function cart()
    {
        return view('home.shopcart');
    }

    public function shopadd(Request $request)
    {
        // $data = $request->number;
        // return($data);

    }

     public function shopaddas(Request $request)
    {
        // echo 111;exit;
        // dd($_GET['id']);
        // $data = $request->id;
        // echo $data;
        // dd($request->sid);
        // dd(123);
        $res = DB::table('goods')->where('id',$request->id)->first();
        Cart::add(['id'=>$res->id, 'name'=>$res->goodsname, 'qty'=>1,'price'=>$res->price,'options'=>['size'=>$res->sid]]);
        echo json_encode($res);
       
        // return view('home.shopercenter',['asc'=>$res]);
    }

    

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        
    }


    public function home()
    {
        if(!empty($_GET['province'])){
            $id = DB::table('base_area')
            ->where('name',$_GET['province'])
            ->first();

            $city = DB::table('base_area')
            ->where('parentid',$id->base_areaid)
            ->get();
            echo json_encode($city);exit;
        }

        if(!empty($_GET['city'])){
            $id = DB::table('base_area')
            ->where('name',$_GET['city'])
            ->first();

            $county = DB::table('base_area')
            ->where('parentid',$id->base_areaid)
            ->get();
            echo json_encode($county);exit;
        }
        

        $res = DB::table('base_area')
        ->where('parentid',0)
        ->get();
        // dd($city);
        return view('home.shouye',['res'=>$res]);
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }


    /**
     *商品详情
     *
     */
    public function goodscenter(Request $request)
    {
        $sid = $request->sid;

        $data = DB::select("select * from goods where id='$sid'");
        return $data;
        // echo json_encode($data);
    }

    public function shopercenters()
    {
          // 获取店铺ID
        $id = $_GET['id'];
        // 获取店铺详情
        $rs = DB::select("select * from shops where id='$id'");

        return view('home.shopercenter',['asc'=>$rs]);
    }
    
    /**
     *点击收藏
     *
     */
    public function shoucang(Request $request)
    {

        $sid = $request->did;
        $uid = $request->uid;

        $aa = DB::select("select * from collects where sid='$sid' and uid='$uid'");
        if(empty($aa)){
            DB::table('collects')->insert(['sid'=>$sid,'uid'=>$uid]);
            return 1;
        }else{
            DB::table('collects')->where('id',$aa[0]->id)->delete();
            return 0;
        }

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function del()
    {
        Cart :: destroy();
        return back();
    }
}
